package com.fyzk.activity.lottery.basketball.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.fyzk.activity.lottery.basketball.SFC_Sel_Pop;
import com.fyzk.adapter.JCLQBaseAdapter;
import com.fyzk.adapter.JCLQSFCAdapter;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.utils.ScreenUtils;
import com.fyzk.utils.Utils;

/**
 * 胜分差玩法
 * 
 * @author miao
 * 
 */
public class BasketballSFCFragment extends BasketballWFBaseFragment implements JCLQBaseAdapter.JLPopOpenListener{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public String getTextStr() {
		// TODO Auto-generated method stub
		return "胜分差";
	}

	@Override
	protected int getLotteryId() {
		// TODO Auto-generated method stub
		return LotteryIDUtils.JLSFC_LOTTERY_ID_INT;
	}




	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (intent != null) {
			JCLQMatchData data = (JCLQMatchData) intent
					.getSerializableExtra("sfc");
			for (int i = 0; i < infos_all.size(); i++) {
				for (int j = 0; j < infos_all.get(i).matchDatas.size(); j++) {
					JCLQMatchData info = infos_all.get(i).matchDatas.get(j);
					if (info.sfc_isZhanKai) {
						data.sfc_isZhanKai = false;
						info.clickNum = data.clickNum;
						infos.get(i).matchDatas.set(j, data);
						info.sfc_isZhanKai = false;
						break;
					}
				}
			}
			adapterChange();
			activity.onBetChecked();
		}else{
			for (int i = 0; i < infos_all.size(); i++) {
				for (int j = 0; j < infos_all.get(i).matchDatas.size(); j++) {
					JCLQMatchData info = infos_all.get(i).matchDatas.get(j);
					info.sfc_isZhanKai= false;
				}
			}
		}
	}
	
	

	public int countChangCi() {
		int changCi = 0;
		for (int i = 0; i < infos_all.size(); i++) {
			for (int j = 0; j < infos_all.get(i).matchDatas.size(); j++) {
				JCLQMatchData data = infos_all.get(i).matchDatas.get(j);
				if (data.clickNum>0) {
					changCi++;
				}
			}
		}
		return changCi;

	}

	@Override
	protected void adapterChange() {
		// TODO Auto-generated method stub
		if (adapter == null) {
			adapter = new JCLQSFCAdapter(activity, infos);
			adapter.setOnJCLQTongJiListener(activity);
			exListView.setAdapter(adapter);
			for (int i = 0; i < adapter.getGroupCount(); i++) {
				exListView.expandGroup(i);
			}
		} else {
			adapter.setList(infos);
		}
		adapter.SetJLPopOpenListener(this);
	}

	@Override
	public void open(JCLQMatchData data) {
		int height = Utils.dip2px(400f);
		SFC_Sel_Pop bf_sel_pop = new SFC_Sel_Pop(getContext(), WindowManager.LayoutParams
				.MATCH_PARENT, height, data);
		LogUtil.v("y=" + (ScreenUtils.getInstance().getHeight() - bf_sel_pop.getHeight()));
		LogUtil.v("p_height=" + bf_sel_pop.getHeight());
		setBackgroundAlpha(0.5f);
		bf_sel_pop.showAtLocation(getView(), Gravity.NO_GRAVITY, 0, ScreenUtils.getInstance()
				.getHeight() -
				height);
		bf_sel_pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				setBackgroundAlpha(1.0f);
				adapterChange();
			}
		});


	}

	public void setBackgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = getActivity().getWindow()
				.getAttributes();
		lp.alpha = bgAlpha;
		getActivity().getWindow().setAttributes(lp);
	}




}
